package pers.crayon.baseprogram.config;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.FormattingConverter;
import ch.qos.logback.core.pattern.color.ANSIConstants;
import org.springframework.context.annotation.Configuration;

/**
 * 点击 Edit Configurations ,在Configuration 中 Environment ——>VM options写入下面配置：
 * -Dspring.output.ansi.enabled=ALWAYS
 * 启动彩色参数配置 ,也可以在配置文件中配置 output
 * 此配置优先级低于logback.xml
 * <p>
 * 原来的继承为
 * ForegroundCompositeConverterBase<ILoggingEvent>
 * 但是继承层级太多了，有待研究实现
 *
 * @author roamay.com
 * @since 2021/4/14 18:15
 */
@Configuration
public class LogColorConfig extends FormattingConverter<ILoggingEvent> {

    @Override
    public String convert(ILoggingEvent iLoggingEvent) {
        Level level = iLoggingEvent.getLevel();
        switch (level.toInt()) {
            //ERROR等级为红色
            case Level.ERROR_INT:
                return ANSIConstants.RED_FG;
            //WARN等级为黄色
            case Level.WARN_INT:
                return ANSIConstants.YELLOW_FG;
            //INFO等级为蓝色
            case Level.INFO_INT:
                return ANSIConstants.BLUE_FG;
            //DEBUG等级为绿色
            case Level.DEBUG_INT:
                return ANSIConstants.GREEN_FG;
            //其他为默认颜色
            default:
                return ANSIConstants.DEFAULT_FG;
        }
    }


}